package com.ruoyi.purchase.service.impl;

import com.ruoyi.purchase.domain.AllocationDetail;
import com.ruoyi.purchase.domain.AllocationPlan;
import com.ruoyi.purchase.domain.OrderDetail;
import com.ruoyi.purchase.domain.RawProductDetail;
import com.ruoyi.purchase.mapper.*;
import com.ruoyi.purchase.service.IAllocationPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * AllocationPlanService业务层处理
 * 
 * @author kevinness
 * @date 2025-01-17
 */
@Service
public class AllocationPlanService implements IAllocationPlanService
{
    @Autowired
    private AllocationPlanMapper allocationPlanMapper;

    @Autowired
    private AllocationDetailMapper allocationDetailMapper;

    @Autowired
    private RawProductDetailMapper rawProductDetailMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    /**
     * 查询AllocationPlan
     * 
     * @param allocationNo AllocationPlan主键
     * @return AllocationPlan
     */
    @Override
    public AllocationPlan selectAllocationPlanByAllocationNo(String allocationNo)
    {
        return allocationPlanMapper.selectAllocationPlanByAllocationNo(allocationNo);
    }

    /**
     * 查询AllocationPlan列表
     * 
     * @param allocationPlan AllocationPlan
     * @return AllocationPlan
     */
    @Override
    public List<AllocationPlan> selectAllocationPlanList(AllocationPlan allocationPlan)
    {
        return allocationPlanMapper.selectAllocationPlanList(allocationPlan);
    }

    /**
     * 新增AllocationPlan
     * 
     * @param allocationPlan AllocationPlan
     * @return 结果
     */
    @Override
    public int insertAllocationPlan(AllocationPlan allocationPlan)
    {
        return allocationPlanMapper.insertAllocationPlan(allocationPlan);
    }

    /**
     * 修改AllocationPlan
     * 
     * @param allocationPlan AllocationPlan
     * @return 结果
     */
    @Override
    public int updateAllocationPlan(AllocationPlan allocationPlan)
    {
        return allocationPlanMapper.updateAllocationPlan(allocationPlan);
    }

    /**
     * 批量删除AllocationPlan
     * 
     * @param allocationNos 需要删除的AllocationPlan主键
     * @return 结果
     */
    @Override
    public int deleteAllocationPlanByAllocationNos(String[] allocationNos)
    {
        for (String allocationNo : allocationNos){
            List<AllocationDetail> allocationDetails = allocationDetailMapper.selectAllocationDetailByAllocationNo(allocationNo);
            for (AllocationDetail allocationDetail : allocationDetails){
                OrderDetail orderDetail = orderDetailMapper.selectOrderDetailByRawProductBatch(allocationDetail.getRawProductBatch());
                orderDetail.setAllocationQuantity(orderDetail.getAllocationQuantity() - allocationDetail.getWeight());
                orderDetailMapper.updateOrderDetail(orderDetail);
            }
        }
        return allocationPlanMapper.deleteAllocationPlanByAllocationNos(allocationNos);
    }

    /**
     * 删除AllocationPlan信息
     * 
     * @param allocationNo AllocationPlan主键
     * @return 结果
     */
    @Override
    public int deleteAllocationPlanByAllocationNo(String allocationNo)
    {
        return allocationPlanMapper.deleteAllocationPlanByAllocationNo(allocationNo);
    }

    /**
     * 根据订单编号获取调拨计划
     *
     * @param orderNo 订单编号
     * @return 调拨计划列表
     */
    @Override
    public List<AllocationPlan> selectAllocationPlanByOrderNo(String orderNo){
        return allocationPlanMapper.selectAllocationPlanByOrderNo(orderNo);
    }
}
